package com.csc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csc.constants.Constants;
import com.csc.domain.Transaction;
import com.csc.domain.User;
import com.csc.service.AccountService;
import com.csc.service.TransactionService;
import com.csc.service.UserService;

@Controller
public class AdminController {
	
	final static Logger logger = Logger.getLogger(AdminController.class);
	
	@Autowired
	private TransactionService transactionService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private UserService userService;	
	
	/**
	 * REVIEW TRANSACTION
	 */

	// TrangNguyen - Review all transaction are pending
	@RequestMapping(value = "/adminReviewTransaction", method=RequestMethod.GET)
	public String doReviewTransaction(HttpServletRequest request, Model model) {
		
		// Get list transaction state Pending
		List<Transaction> listTransaction = transactionService.getTransactionListPending();
		if(listTransaction.size() > 0){
			model.addAttribute("listTransaction", listTransaction);			
		}
		
		return "adminReviewTransactionPage";
	}
	
	/**
	 * APPROVE TRANSACTION
	 */
	
	// TrangNguyen - return adminApproveTransactionPage.jsp page with validation
	@RequestMapping(value="/adminApproveTransaction", method=RequestMethod.GET)	
	public String doViewTransactionWillApprove(HttpServletRequest request, Model model){
		
		String transactionId = request.getParameter("transactionId");		
		
		logger.info("Start view Transaction will approve : transactionId = " +transactionId);
				
		String validation = transactionService.verifyTransaction(transactionId);
		
		// Verify and view transaction with validation		
							
		if (validation.equals("")) {
			model.addAttribute("titleOK", "This transaction can approve");					
		} else {
			model.addAttribute("titleError", "Can't approve this transaction");
			model.addAttribute("error", validation);
			if (validation.equals(Constants.ERROR_PARSE_INT) || validation.equals(Constants.ERROR_TRANSACTION_NOT_FOUND)) {
				return "adminApproveTransactionPage";
			}						
		}
		
		model.addAttribute("transaction", transactionService.findTransactionById(Integer.parseInt(transactionId)));
		return "adminApproveTransactionPage";
	}
	
	// TrangNguyen - return 
	@RequestMapping(value="/adminApproveTransactionComplete", method=RequestMethod.POST)
	public String doConfirmApprove(HttpServletRequest request, Model model) {
		
		String transactionId = request.getParameter("transactionId");	

		logger.info("Confirm Approve Transaction ID = " + transactionId);

		String verify = transactionService.verifyTransaction(transactionId);
				
		if (!verify.equals("")) {
			model.addAttribute("transactionId", transactionId);		
			return "redirect:/adminApproveTransaction";
		}
			
		HttpSession session = request.getSession();
		User userAdmin = (User) session.getAttribute("user");
		
		String transactionComment = request.getParameter("transactionComment");		
		if (transactionComment.length() > 45) transactionComment = transactionComment.substring(0,45);
		
		if (transactionService.approve(transactionId, userAdmin, transactionComment)) {
			model.addAttribute("success", "Transaction Id = " + transactionId + " is approved" );
			model.addAttribute("transaction", transactionService.findTransactionById(Integer.parseInt(transactionId)));
			
		} else {
			model.addAttribute("errorSQL", "SQL update error");				
		}			
		
		return "adminApproveTransactionCompletePage";
		
	}
	
	/**
	 * REJECT TRANSACTION
	 */
	
	// TrangNguyen
	@RequestMapping(value="/adminRejectTransaction", method=RequestMethod.GET)
	public String doViewTransactionWillReject(HttpServletRequest request, Model model) {
		String transactionId = request.getParameter("transactionId");		
		
		logger.info("Start view Transaction will reject : transactionId = " +transactionId);
				
		String verify = transactionService.verifyTransaction(transactionId);
		
		// Verify transaction
		if (verify.equals(Constants.ERROR_PARSE_INT) || verify.equals(Constants.ERROR_TRANSACTION_NOT_FOUND)) {				
			model.addAttribute("error", verify);
			return "adminRejectTransactionPage";	
		} 
		
		if (verify.equals("")){
			model.addAttribute("titleOK", "This transaction can approve");
		}
		
		model.addAttribute("error", verify);
		model.addAttribute("transaction", transactionService.findTransactionById(Integer.parseInt(transactionId)));
	
		return "adminRejectTransactionPage";

	}
	
	// TrangNguyen
	@RequestMapping(value="/adminRejectTransactionComplete", method=RequestMethod.POST)
	public String doConfirmReject(HttpServletRequest request, Model model){
		
		String transactionId = request.getParameter("transactionId");
		
		logger.info("Process Reject Transaction ID = " + transactionId);
		
		String verify = transactionService.verifyTransaction(transactionId);
		if (verify.equals(Constants.ERROR_PARSE_INT) || verify.equals(Constants.ERROR_TRANSACTION_NOT_FOUND)) {				
			model.addAttribute("error", verify);
			return "redirect:/adminRejectTransactionPage.html";	
		} 
		
		HttpSession session = request.getSession();
		User userAdmin = (User) session.getAttribute("user");
		
		String transactionComment = request.getParameter("transactionComment");
		if (transactionComment.length() > 45) transactionComment = transactionComment.substring(0,45);
		
		if (transactionService.reject(transactionId, userAdmin, transactionComment)) {
			model.addAttribute("success", "Transaction Id = " + transactionId + " is rejected" );
			model.addAttribute("transaction", transactionService.findTransactionById(Integer.parseInt(transactionId)));
		} else {
			model.addAttribute("errorSQL", "SQL update error");
		}		
		
		return "adminRejectTransactionCompletePage";
		
	}
	
	/**
	 * VIEW TRANSACTION
	 */
	
	// TrangNguyen
	@RequestMapping(value="/adminViewTransaction", method=RequestMethod.GET)
	public String doViewTransaction(HttpServletRequest request, Model model){
		
		String transactionId = request.getParameter("transactionId");	
		
		String verify = transactionService.verifyTransaction(transactionId);
		
		if (verify.equals(Constants.ERROR_PARSE_INT) || verify.equals(Constants.ERROR_TRANSACTION_NOT_FOUND)) {				
			model.addAttribute("error", verify);
			return "adminViewTransactionPage";
		} 
		
		model.addAttribute("transaction", transactionService.findTransactionById(Integer.parseInt(transactionId)));
		return "adminViewTransactionPage";
	}
	
	/**
	 * VIEW COMPLETE TRANSACTION
	 */
	
	// TrangNguyen - view all transaction are complete
	@RequestMapping(value = "/adminCompleteTransaction", method=RequestMethod.GET)
	public String doViewCompleteTransaction(HttpServletRequest request, Model model) {
		
		// Get list transaction state Pending
		List<Transaction> listTransaction = transactionService.getTransactionListComplete();
		if(listTransaction.size() > 0){
			model.addAttribute("listTransaction", listTransaction);			
		}
		
		return "adminCompleteTransactionPage";
	}

	// TrangNguyen - return adminReviewTransaction controller
	@RequestMapping(value="/adminClose")
	public String doAdminClose(){
		return "redirect:/adminReviewTransaction.html";
	}
}
